package com.gsafety.wellsfort.service.task;

import com.gsafety.wellsfort.domain.dto.push.PushInfoDTO;
import com.gsafety.wellsfort.domain.model.course.LessonPeriod;
import com.gsafety.wellsfort.domain.model.security.UserLogin;
import com.gsafety.wellsfort.service.common.IPushService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Slf4j
public class LessonPeriodPublishTask extends Task {

    /**
     * 执行时间(单位：毫秒数)
     */
    private long executeTime;

    /**
     * push 服务
     */
    private IPushService pushService;

    /**
     * 推送用户
     */
    private List<UserLogin> userLoginList;

    /**
     * 课时信息
     */
    private LessonPeriod lessonPeriod;

    public LessonPeriodPublishTask(long executeTime, IPushService pushService, List<UserLogin> userLoginList, LessonPeriod lessonPeriod) {
        this.executeTime = executeTime;
        this.pushService = pushService;
        this.userLoginList = userLoginList;
        this.lessonPeriod = lessonPeriod;
    }

    @Override
    public void doWork() {
        log.info("开始执行任务..");
        PushInfoDTO pushInfoDTO = PushInfoDTO.builder().title(lessonPeriod.getName() + "课时即将上线").
                message("还有24小时," + lessonPeriod.getName() + "课时上线").deviceType("ALL").userLoginList(userLoginList).build();
        pushService.push(pushInfoDTO);
        log.info("任务执行结束..");
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(executeTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return this.compareTo(o);
    }
}
